package com.gec.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.common.BaseContext;
import com.gec.entity.Dish;
import com.gec.entity.ShoppingCart;
import com.gec.mapper.ShoppingCartMapper;
import com.gec.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.OutputStream;

/**
 * <p>
 *      购物车服务接口
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/15 20:17]
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    @Transactional
    public ShoppingCart add(ShoppingCart shoppingCart) {
        // 设置用户ID
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        // 获取菜品ID
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 构造查询条件
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        // 判断菜品ID是否为空
        if (!ObjectUtils.isEmpty(dishId)){
            // 添加查询条件 --> 是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            // 是套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 查询是否已经存在与购物车
        ShoppingCart cart = this.getOne(shoppingCartLambdaQueryWrapper);
        if (!ObjectUtils.isEmpty(cart)){
            // 购物车已存在该套餐
            Integer number = cart.getNumber();
            // 数量+1
            number += 1;
            cart.setNumber(number);
            // 更新数据
            this.updateById(cart);
        }else {
            // 不存在该菜品
            // 菜品数量
            shoppingCart.setNumber(1);
            // 插入数据
            this.save(shoppingCart);
        }
        return shoppingCart;
    }

    @Override
    @Transactional
    public void clear() {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 构造条件
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        // 删除
        this.remove(shoppingCartLambdaQueryWrapper);
    }

    @Override
    @Transactional
    public void sub(ShoppingCart shoppingCart) {
        LambdaUpdateWrapper<ShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        shoppingCartLambdaUpdateWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());


        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 构造查询条件
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        // 菜品ID不为空,则减少的是菜品 否则为套餐
        if (!ObjectUtils.isEmpty(shoppingCart.getDishId())){
            // 菜品ID
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            shoppingCartLambdaUpdateWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            // 套餐ID
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            shoppingCartLambdaUpdateWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 查询
        ShoppingCart cart = this.getOne(shoppingCartLambdaQueryWrapper);
        // 获取当前菜单或套餐数量
        Integer number = cart.getNumber();
        if (number == 1){
            // 删除该条记录
            this.remove(shoppingCartLambdaQueryWrapper);
            return;
        }
        shoppingCartLambdaUpdateWrapper.set(ShoppingCart::getNumber,number - 1);
        this.update(shoppingCartLambdaUpdateWrapper);
    }
}
